package org.jeecg.modules.tankerRules.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.tankerRules.entity.TankerRules;
import org.jeecg.modules.tankerRules.service.ITankerRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 油罐车入场规则
 * @Author: jeecg-boot
 * @Date:   2024-07-05
 * @Version: V1.0
 */
@Api(tags="油罐车入场规则")
@RestController
@RequestMapping("/tankerRules/tankerRules")
@Slf4j
public class TankerRulesController extends JeecgController<TankerRules, ITankerRulesService> {
	@Autowired
	private ITankerRulesService tankerRulesService;
	 @Autowired
	 private ISysDepartService departService;
	
	/**
	 * 分页列表查询
	 *
	 * @param tankerRules
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "油罐车入场规则-分页列表查询")
	@ApiOperation(value="油罐车入场规则-分页列表查询", notes="油罐车入场规则-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TankerRules tankerRules,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TankerRules> queryWrapper = QueryGenerator.initQueryWrapper(tankerRules, req.getParameterMap());
		Page<TankerRules> page = new Page<TankerRules>(pageNo, pageSize);
		IPage<TankerRules> pageList = tankerRulesService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询
	  *
	  * @return
	  */
	 @AutoLog(value = "油罐车入场规则-根据公司名称查询")
	 @ApiOperation(value="油罐车入场规则-根据公司名称查询", notes="油罐车入场规则-根据公司名称查询")
	 @GetMapping(value = "/getByLocation")
	 public Result<?> getByLocation() {
		 LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<SysDepart> sysDeparts = departService.queryUserDeparts(user.getId());
		 if (sysDeparts.isEmpty()){
			 return Result.error("用户尚未归属部门,请选择部门登录");
		 }
		 String departName = sysDeparts.get(0).getDepartName();
		 LambdaQueryWrapper<TankerRules> wrapper = Wrappers.lambdaQuery();
		 wrapper.eq(TankerRules::getLocation, departName);
		 TankerRules one = tankerRulesService.getOne(wrapper);
		 if (one == null){
			 TankerRules tankerRules = new TankerRules();
			 tankerRules.setLocation(departName).setRegisterTime(12).setAuditTime(12).setCheckTime(0)
							 .setRegister(true).setAudit(true).setCheckEnable(true);
			 tankerRulesService.save(tankerRules);
			 return Result.OK(tankerRules);
		 }
		 return Result.OK(one);
	 }
	
	/**
	 *   添加
	 *
	 * @param tankerRules
	 * @return
	 */
	@AutoLog(value = "油罐车入场规则-添加")
	@ApiOperation(value="油罐车入场规则-添加", notes="油罐车入场规则-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TankerRules tankerRules) {
		tankerRulesService.save(tankerRules);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param tankerRules
	 * @return
	 */
	@AutoLog(value = "油罐车入场规则-编辑")
	@ApiOperation(value="油罐车入场规则-编辑", notes="油罐车入场规则-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TankerRules tankerRules) {
		tankerRulesService.updateById(tankerRules);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "油罐车入场规则-通过id删除")
	@ApiOperation(value="油罐车入场规则-通过id删除", notes="油罐车入场规则-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		tankerRulesService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "油罐车入场规则-批量删除")
	@ApiOperation(value="油罐车入场规则-批量删除", notes="油罐车入场规则-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tankerRulesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "油罐车入场规则-通过id查询")
	@ApiOperation(value="油罐车入场规则-通过id查询", notes="油罐车入场规则-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		TankerRules tankerRules = tankerRulesService.getById(id);
		if(tankerRules==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tankerRules);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tankerRules
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TankerRules tankerRules) {
        return super.exportXls(request, tankerRules, TankerRules.class, "油罐车入场规则");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TankerRules.class);
    }

}
